<!-- <template>
	<view class="box">
		<view class="content" :style="{'height':(device.windowHeight-tabHeightPX-device.footerHeight)+'px;', 'margin-top':'10px;'}">
			<view v-if="current == 1">
				<u-empty mode="data" v-if="history.length == 0"></u-empty>
			</view>
			<view id="id-invoice-list" v-else-if="dataDict.hasOwnProperty(current)" class="list">
				<view class="item" v-for="(i, index) in dataDict[current]" :key="i.id" @click="selectVoice(i)">
					<view class="item_header">
						{{i.invoiceTypeCode == 'GENERAL_VAT'?'普通发票':'专用发票'}}-{{i.invoiceTitleType == 1?'个人':'单位'}}
					</view>
					<view class="item_content">
						<text>{{i.invoiceTitle}}</text>
						<view class="item_handle">
							<u-icon name="edit-pen" size="28" @click.stop="toEdit(i)"></u-icon>
							<u-icon name="trash" size="28" @click.stop="del(i)"></u-icon>
						</view>
					</view>
				</view>
				<u-empty mode="data" v-if="dataDict[current].length == 0"></u-empty>
				<x-footer>
					<button style="width:710rpx; border: none; border-radius: 44rpx;" class="x-btn" @click="addInvoice">添加发票抬头</button>
				</x-footer>
			</view>
		</view>
	</view>
</template> -->
<template>
	<view class="x-list-content">
		<template v-if="isLoading">
			<x-loading></x-loading>
		</template>
		<template v-else-if="!isLoading && datas && datas.length>0">
			<scroll-view scroll-y="true" :style="{height:(device.windowHeight)+'px'}">
			 	<template v-for="(item, index) in datas">
					<view :class="[itemClasses[index], 'element']" @click="selectVoice(item)">
						<view class="info">
							<view class="header">
								{{item.invoiceTypeCode == 'GENERAL_VAT'?'普通发票':'专用发票'}}-{{item.invoiceTitleType == 1?'个人':'单位'}}
							</view>
							<view class="content">{{item.invoiceTitle}}</view>
						</view>
						<view class="edit">
							<!-- <u-icon customStyle="padding: 20rpx;" @click.stop="toEdit(item)" name="edit-pen"
								:color="xIconColor()" size="24"></u-icon>
							<u-icon customStyle="marginLeft:20rpx; padding: 20rpx;" @click.stop="del(item)"
								name="trash" :color="xIconColor()" size="24"></u-icon> -->
						</view>
					</view>
					<view v-if="index==datas.length-1" class="x-list-bottom-seperate-line"></view>
				</template>
			</scroll-view>
			<!-- <x-footer>
				<button class="x-btn" style="width:710rpx;border: 'none'; border-radius: 44rpx;"
					@click="addInvoice">添加发票抬头</button>
			</x-footer> -->
		</template>
		<template v-else>
			<x-empty class="x-empty" :height="device.windowHeight"></x-empty>
		</template>
	</view>
</template>

<script>
	import {
		page,
		del
	} from '@/api/invoice.js'
	import EventBusMacro from '@/plugins/EventBus/eventBusMacro.js'
	import {
		mapState
	} from 'vuex'
	import {
		ThemeMixin
	} from '@/mixins/theme/xThemeMixin.js'
	
	export default {
		components:{
		},
		mixins: [ThemeMixin],
		computed: {
			...mapState(['device']),
			itemClasses: function() {
				return this.datas.map((item, index) => {
					if (index === 0) {
						return 'x-list-item-first';
					} else if (index === this.datas.length - 1) {
						return 'x-list-item-last';
					} else {
						return 'x-list-item';
					}
				});
			},
		},
		data() {
			return {
				// current: 0,
				datas: [], //存放数据，一旦有数据就缓存起来，直到退出页面才失效
				// offsetDict: {}, //存放数据偏移量，切换回页面不用从头开始
				// history: [],
				isSelected: false,
				// tabHeightPX: 50,
				isLoading: true,
			}
		},
		onLoad(options) {
			if (options.isSelected) {
				this.isSelected = JSON.parse(options.isSelected);
			}
			this.loadData()
			this.$events.on(EventBusMacro.EB_EDIT_INVOICE_INFO, this.onEditInvoice)
		},
		beforeDestroy() {
			this.$events.off(EventBusMacro.EB_EDIT_INVOICE_INFO, this.onEditInvoice)
		},
		// onPageScroll(event) {
		// 	// 记录当前滚动位置
		// 	console.log("scrollTop=", event.scrollTop)
		// 	this.offsetDict[this.current] = event.scrollTop
		// },

		methods: {
			onEditInvoice() {
				this.loadData()
			},
			// changeTab(e) {
			// 	this.current = e
			// 	if (this.dataDict.hasOwnProperty(this.current)) {
			// 		let result = this.dataDict[this.current]
			// 		if (Array.isArray(result) && result.length > 0) {
			// 			this.$nextTick(() => {
			// 				uni.pageScrollTo({
			// 					scrollTop: this.offsetDict[this.current],
			// 					duration: 0, // 滚动所需时间，默认值为300ms
			// 				})
			// 			})
			// 			return;
			// 		}
			// 	}

			// 	if (e == 0) {
			// 		this.loadData()
			// 	}
			// },
			selectVoice(e) {
				if (!this.isSelected) {
					return false;
				}
				this.$events.emit(EventBusMacro.EB_INVOICE_CHANGE, e);
				uni.navigateBack()
			},
			addInvoice(e) {
				uni.navigateTo({
					url: '/pages/mine/invoice/add',
					complete: c => {
						console.log('c', c)
					}
				})
			},
			loadData() {
				this.isLoading = true
				this.$showLoading()
				page().then(res => {
					this.datas = res
					this.isLoading = false
					this.$hideLoading()
					// this.$set(this.dataDict, this.current, res);
				}).catch(err => {
					this.isLoading = false
					this.$hideLoading()
				})
			},
			toEdit(e) {
				uni.navigateTo({
					url: '/pages/mine/invoice/add?info=' + JSON.stringify(e)
				})
			},
			del(e) {
				uni.showModal({
					title: '提示',
					content: '确定删除这个发票抬头吗？',
					success: res => {
						if (res.confirm) {
							del(e.id).then(d => {
								uni.showToast({
									title: '删除成功',
									icon: 'none'
								})
								this.loadData()
							})
						}
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
 	// <template v-for="(item, index) in datas">
		// 			<view :class="[itemClasses[index], 'element']" @click="selectVoice(item)">
		// 				<view class="info">
		// 					<view class="header">
		// 						{{item.invoiceTypeCode == 'GENERAL_VAT'?'普通发票':'专用发票'}}-{{item.invoiceTitleType == 1?'个人':'单位'}}
		// 					</view>
		// 					<view class="content">{{item.invoiceTitle}}</view>
		// 				</view>
		// 				<view class="edit">
		// 					<u-icon customStyle="padding: 20rpx;" @click.stop="toEdit(item)" name="edit-pen"
		// 						:color="xIconColor()" size="24"></u-icon>
		// 					<u-icon customStyle="marginLeft:20rpx; padding: 20rpx;" @click.stop="del(item)"
		// 						name="trash" :color="xIconColor()" size="24"></u-icon>
		// 				</view>
		// 			</view>
		// 			<view v-if="index==datas.length-1" class="bottom-seperate-line"></view>
		// 		</template>
				.element {
					display: flex;
					align-items: center;
					padding: 20rpx;
					justify-content: space-between;
					
					.info {
						display: flex;
						flex-direction: column;
						.header {
							font-size: 23rpx;
							font-family: PingFangSC-Semibold, PingFang SC;
							font-weight: 600;
							color: #000000;
							line-height: 33rpx;
						}
						.content {
							font-size: 23rpx;
							font-family: PingFangSC-Regular, PingFang SC;
							font-weight: 400;
							color: #8E8E8E;
							line-height: 33rpx;
							margin-right: 4rpx;
						}
					}
						
					.edit {
						display: flex;
						align-items: center;
					}
				}
				.bottom-seperate-line {
					
				}
</style>

<style scoped>
	.item_handle {
		display: flex;
		align-items: center;
	}

	.list {
		padding-top: 10rpx;
	}

	.box {
		background-color: #f1f1f1;
		font-size: 30rpx;
		/* min-height: 100vh; */
		/* padding-bottom: 100rpx; */
	}

	.item {
		width: 100%;
		background-color: #fff;
		margin: 10rpx auto;
		padding: 0 30rpx;
	}

	.item_header {
		width: 100%;
		height: 70rpx;
		line-height: 70rpx;
		font-weight: bold;
		/* border-bottom: 2rpx solid #f1f1f1; */
	}

	.item_content {
		display: flex;
		align-items: center;
		justify-content: space-between;
		height: 100rpx;
	}

	.footer {
		position: fixed;
		border-top: 1px solid #AAA;
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
		padding-top: 8px;

	}
</style>